{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dd01d524",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of Observations: 150\n",
      "Number of Features: 4\n",
      "Number of Classes: 3\n"
     ]
    }
   ],
   "source": [
    "from sklearn import datasets\n",
    "dataset = datasets.load_iris()\n",
    "#dataset = datasets.load_digits()\n",
    "X = dataset.data\n",
    "y = dataset.target\n",
    "\n",
    "observations = len(X)\n",
    "features = len(dataset.feature_names)\n",
    "classes = len(dataset.target_names)\n",
    "print(\"Number of Observations: \" + str(observations))\n",
    "print(\"Number of Features: \" + str(features))\n",
    "print(\"Number of Classes: \" + str(classes))\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7b6366ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===== decision tree ======\n",
      "Training accuracy is 1.0\n",
      "Test accuracy is 0.8666666666666667\n",
      "Labels of all instances:\n",
      "[1 0 2 2 1 0 0 0 1 0 1 2 1 1 0 2 2 0 2 2 1 1 0 0 0 0 2 1 2 2]\n",
      "Predictive outputs of all instances:\n",
      "[1 0 2 1 1 0 0 0 2 0 1 2 2 1 0 2 2 0 2 2 1 2 0 0 0 0 2 1 2 2]\n",
      "Confusion Matrix:\n",
      "[[11  0  0]\n",
      " [ 0  6  3]\n",
      " [ 0  1  9]]\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00        11\n",
      "           1       0.86      0.67      0.75         9\n",
      "           2       0.75      0.90      0.82        10\n",
      "\n",
      "    accuracy                           0.87        30\n",
      "   macro avg       0.87      0.86      0.86        30\n",
      "weighted avg       0.87      0.87      0.86        30\n",
      "\n"
     ]
    }
   ],
   "source": [
    "############## decision tree\n",
    "\n",
    "print(\"===== decision tree ======\")\n",
    "\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "\n",
    "tree = DecisionTreeClassifier()\n",
    "tree.fit(X_train, y_train)\n",
    "print(\"Training accuracy is %s\"% tree.score(X_train,y_train))\n",
    "print(\"Test accuracy is %s\"% tree.score(X_test,y_test))\n",
    "\n",
    "print(\"Labels of all instances:\\n%s\"%y_test)\n",
    "y_pred = tree.predict(X_test)\n",
    "print(\"Predictive outputs of all instances:\\n%s\"%y_pred)\n",
    "\n",
    "from sklearn.metrics import classification_report, confusion_matrix\n",
    "print(\"Confusion Matrix:\\n%s\"%confusion_matrix(y_test, y_pred))\n",
    "print(\"Classification Report:\\n%s\"%classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "69a0dcbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===== k-nn ======\n",
      "Training accuracy is 0.9916666666666667\n",
      "Test accuracy is 0.9\n",
      "Labels of all instances:\n",
      "[1 0 2 2 1 0 0 0 1 0 1 2 1 1 0 2 2 0 2 2 1 1 0 0 0 0 2 1 2 2]\n",
      "Predictive outputs of all instances:\n",
      "[1 0 2 1 1 0 0 0 2 0 1 2 2 1 0 2 2 0 2 2 1 1 0 0 0 0 2 1 2 2]\n",
      "Confusion Matrix:\n",
      "[[11  0  0]\n",
      " [ 0  7  2]\n",
      " [ 0  1  9]]\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00        11\n",
      "           1       0.88      0.78      0.82         9\n",
      "           2       0.82      0.90      0.86        10\n",
      "\n",
      "    accuracy                           0.90        30\n",
      "   macro avg       0.90      0.89      0.89        30\n",
      "weighted avg       0.90      0.90      0.90        30\n",
      "\n"
     ]
    }
   ],
   "source": [
    "############## k-nn\n",
    "\n",
    "print(\"===== k-nn ======\")\n",
    "\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "neigh = KNeighborsClassifier(n_neighbors=3)\n",
    "neigh.fit(X_train, y_train)\n",
    "print(\"Training accuracy is %s\"% neigh.score(X_train,y_train))\n",
    "print(\"Test accuracy is %s\"% neigh.score(X_test,y_test))\n",
    "\n",
    "print(\"Labels of all instances:\\n%s\"%y_test)\n",
    "y_pred = neigh.predict(X_test)\n",
    "print(\"Predictive outputs of all instances:\\n%s\"%y_pred)\n",
    "\n",
    "from sklearn.metrics import classification_report, confusion_matrix\n",
    "print(\"Confusion Matrix:\\n%s\"%confusion_matrix(y_test, y_pred))\n",
    "print(\"Classification Report:\\n%s\"%classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "38fcbdac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===== Logistic Regression ======\n",
      "Training accuracy is 0.9833333333333333\n",
      "Test accuracy is 0.9333333333333333\n",
      "Labels of all instances:\n",
      "[1 0 2 2 1 0 0 0 1 0 1 2 1 1 0 2 2 0 2 2 1 1 0 0 0 0 2 1 2 2]\n",
      "Predictive outputs of all instances:\n",
      "[1 0 2 1 1 0 0 0 2 0 1 2 1 1 0 2 2 0 2 2 1 1 0 0 0 0 2 1 2 2]\n",
      "Confusion Matrix:\n",
      "[[11  0  0]\n",
      " [ 0  8  1]\n",
      " [ 0  1  9]]\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00        11\n",
      "           1       0.89      0.89      0.89         9\n",
      "           2       0.90      0.90      0.90        10\n",
      "\n",
      "    accuracy                           0.93        30\n",
      "   macro avg       0.93      0.93      0.93        30\n",
      "weighted avg       0.93      0.93      0.93        30\n",
      "\n"
     ]
    }
   ],
   "source": [
    "############## Logistic Regression \n",
    "\n",
    "print(\"===== Logistic Regression ======\")\n",
    "\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "reg = LogisticRegression(solver='lbfgs', max_iter=10000)\n",
    "reg.fit(X_train, y_train)\n",
    "print(\"Training accuracy is %s\"% reg.score(X_train,y_train))\n",
    "print(\"Test accuracy is %s\"% reg.score(X_test,y_test))\n",
    "\n",
    "print(\"Labels of all instances:\\n%s\"%y_test)\n",
    "y_pred = reg.predict(X_test)\n",
    "print(\"Predictive outputs of all instances:\\n%s\"%y_pred)\n",
    "\n",
    "from sklearn.metrics import classification_report, confusion_matrix\n",
    "print(\"Confusion Matrix:\\n%s\"%confusion_matrix(y_test, y_pred))\n",
    "print(\"Classification Report:\\n%s\"%classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "91b4362e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===== Naive Bayes ======\n",
      "Training accuracy is 0.9666666666666667\n",
      "Test accuracy is 0.9333333333333333\n",
      "Labels of all instances:\n",
      "[1 0 2 2 1 0 0 0 1 0 1 2 1 1 0 2 2 0 2 2 1 1 0 0 0 0 2 1 2 2]\n",
      "Predictive outputs of all instances:\n",
      "[1 0 2 1 1 0 0 0 1 0 1 2 1 1 0 2 2 0 2 2 1 2 0 0 0 0 2 1 2 2]\n",
      "Confusion Matrix:\n",
      "[[11  0  0]\n",
      " [ 0  8  1]\n",
      " [ 0  1  9]]\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00        11\n",
      "           1       0.89      0.89      0.89         9\n",
      "           2       0.90      0.90      0.90        10\n",
      "\n",
      "    accuracy                           0.93        30\n",
      "   macro avg       0.93      0.93      0.93        30\n",
      "weighted avg       0.93      0.93      0.93        30\n",
      "\n"
     ]
    }
   ],
   "source": [
    "############## Naive Bayes\n",
    "\n",
    "print(\"===== Naive Bayes ======\")\n",
    "\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "gnb = GaussianNB()\n",
    "gnb.fit(X_train, y_train)\n",
    "print(\"Training accuracy is %s\"% gnb.score(X_train,y_train))\n",
    "print(\"Test accuracy is %s\"% gnb.score(X_test,y_test))\n",
    "\n",
    "print(\"Labels of all instances:\\n%s\"%y_test)\n",
    "y_pred = gnb.predict(X_test)\n",
    "print(\"Predictive outputs of all instances:\\n%s\"%y_pred)\n",
    "\n",
    "from sklearn.metrics import classification_report, confusion_matrix\n",
    "print(\"Confusion Matrix:\\n%s\"%confusion_matrix(y_test, y_pred))\n",
    "print(\"Classification Report:\\n%s\"%classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6793a070",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73e9b2c7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ff6aaf2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
